package com.zjun.demo.experimentaldemo.gallery_monitoring

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.zjun.demo.experimentaldemo.R

class EventAdapter(
    private val onGroupClick: (EventGroup) -> Unit,
    private val onItemClick: (Event) -> Unit
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    var currentList = mutableListOf<EventGroup>()

    companion object {
         const val TYPE_HEADER = 0
         const val TYPE_ITEM = 1
    }

    private val displayItems = mutableListOf<DisplayItem>()

    fun submitList(groups: List<EventGroup>) {
        displayItems.clear()
        groups.forEach { group ->
            displayItems.add(DisplayItem(TYPE_HEADER, group = group))
            if (group.isExpanded) {
                group.events.forEach { event ->
                    displayItems.add(DisplayItem(TYPE_ITEM, event = event))
                }
            }
        }
        notifyDataSetChanged()
    }

    override fun getItemViewType(position: Int) = displayItems[position].type

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            TYPE_HEADER -> GroupHeaderViewHolder(
                LayoutInflater.from(parent.context)
                    .inflate(R.layout.item_group_header, parent, false)
            )
            else -> EventItemViewHolder(
                LayoutInflater.from(parent.context)
                    .inflate(R.layout.item_event, parent, false)
            )
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is GroupHeaderViewHolder -> {
                holder.bind(displayItems[position].group!!, onGroupClick)
            }
            is EventItemViewHolder -> {
                holder.bind(displayItems[position].event!!, onItemClick)
            }
        }
    }

    override fun getItemCount() = displayItems.size

    inner class GroupHeaderViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        fun bind(group: EventGroup, onClick: (EventGroup) -> Unit) {
            itemView.apply {
                findViewById<TextView>(R.id.tvHour).text = group.hour
                findViewById<ImageView>(R.id.ivExpand).setImageResource(
                    if (group.isExpanded) R.drawable.ic_expand else R.drawable.ic_collapse
                )
                setOnClickListener { onClick(group) }
            }
        }
    }

    inner class EventItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        fun bind(event: Event, onClick: (Event) -> Unit) {
            itemView.apply {
                // 绑定事件数据到视图
                findViewById<TextView>(R.id.tvEventId).text = event.id
                Glide.with(context).load(event.icon).into(findViewById(R.id.ivEvent))
                setOnClickListener { onClick(event) }
            }
        }
    }
}